package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.constants.QueueConstants;
import com.ytjj.common.dto.mqdto.PrepaidRefillDto;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.dao.RechargeDao;
import com.ytjj.qmyx.admin.model.request.RechargeRequest;
import com.ytjj.qmyx.admin.model.response.RechargeListResponse;
import com.ytjj.qmyx.admin.service.RechargeService;
import com.ytjj.qmyx.mall.mapper.PhoneRechargeMapper;
import com.ytjj.qmyx.mall.model.PhoneRecharge;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 *
 *  *
 **/
@Service
public class RechargeServiceImpl implements RechargeService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private RechargeDao rechargeDao;

    @Resource
    private PhoneRechargeMapper phoneRechargeMapper;

    @Override
    public List<RechargeListResponse> list(RechargeRequest rechargeRequest) {
        PageHelper.startPage(rechargeRequest.getPageNum(),rechargeRequest.getPageSize());
        return rechargeDao.findList(rechargeRequest);
    }

    @Override
    public void add(Integer id) {
        RechargeListResponse response = rechargeDao.findByOrderInfoId(id);
        if(response.getActivityMin().intValue() != 0)  return;
        int price = response.getActivityMax().multiply(new BigDecimal(100)).intValue();
        PhoneRecharge phoneRecharge = new PhoneRecharge();
        phoneRecharge.setOrderNo(response.getOrderNo());
        phoneRecharge.setPhone(response.getUserPhone());
        phoneRecharge.setOperators(getPhoneOperators(response.getUserPhone()));
        phoneRecharge.setPlatform("");
        phoneRecharge.setChannel("");
        phoneRecharge.setUuid(id+"");
        phoneRecharge.setStatus(3);                         // 支付成功
        phoneRecharge.setPayType("");
        phoneRecharge.setDenomination(price);
        phoneRecharge.setGiveIntegral(0);
        phoneRecharge.setPrice(price);
        phoneRecharge.setErrorStatus(0);
        phoneRecharge.setCreateTime(new Date());
        phoneRecharge.setType("RECHARGE_COUPON");
        phoneRechargeMapper.insert(phoneRecharge);
        rechargeDao.updateInfoStatus(id);

        PrepaidRefillDto prepaidRefillDto = new PrepaidRefillDto();
        prepaidRefillDto.setOrderNo(response.getOrderNo());
        prepaidRefillDto.setOperators(phoneRecharge.getOperators());
        // 发送队列
        rabbitTemplate.convertAndSend(QueueConstants.RECHARGE_QUEUE, prepaidRefillDto);
    }

    public static String getPhoneOperators(String phone) {
        String s = HttpClientUtil.doGet("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+phone.trim());
        if(s.contains("电信")) return "03";
        if(s.contains("移动")) return "02";
        if(s.contains("联通")) return "01";
        return "";
    }
}
